/*
 * ====================================================================
 * Copyright (C) 2013 Eric Blond
 *
 * This file is part of Neptune.
 *
 * Neptune is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Neptune is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Neptune.  If not, see <http://www.gnu.org/licenses/>.
 * ====================================================================
 */

package com.hyperplan.neptune.utils;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class Block {

	private List<String> lines;
	
	public Block(String text) {
		lines = new ArrayList<String>();
		lines.add(text);
	}
	
	public Block() {
		this(new ArrayList<String>());
	}
	
	public Block(List<String> lines) {
		this.lines = lines;
	}
	
	public Block(Block block) {
		this.lines = new ArrayList<String>(block.getLines());
	}
	
	public List<String> getLines() {
		return lines;
	}
	
	public int getWidth() {
		int maxWidth = 0;
		for (String line : lines) {
			if (line.length() > maxWidth) {
				maxWidth = line.length();
			}
		}
		return maxWidth;
	}
	
	public int getHeight() {
		return lines.size();
	}
	
	public void addLeft(String text) {
		for (int i = 0; i < lines.size(); i++) {
			lines.set(i, text + lines.get(i));
		}
	}
	
	private static String completedLine(String line, int length) {
		if (line.length() < length) {
			return line + blankLine(length - line.length());
		} else {
			return line;
		}
	}
	
	private static String blankLine(int length) {
		return regularLine(' ', length);
	}
	
	private static String regularLine(char character, int length) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			buffer.append(character);
		}
		return buffer.toString();
	}
	
	public void addRight(String text) {
		int width = getWidth();
		for (int i = 0; i < lines.size(); i++) {
			lines.set(i, completedLine(lines.get(i), width) + text);
		}
	}
	
	public void addVertically(Block block) {
		lines.addAll(block.getLines());
	}
	
	public void addDiagonally(Block block) {
		if (lines.isEmpty()) {
			lines.addAll(block.getLines());
		} else if (!block.getLines().isEmpty()) {
			int height = getHeight();
			String prefix = blankLine(lines.get(height - 1).length());
			lines.set(height - 1, lines.get(height - 1) + block.getLines().get(0));
			for (int i = 1; i < block.getLines().size(); i++) {
				lines.add(prefix + block.getLines().get(i));
			}
		}
	}

	public void addTop(char character) {
		lines.add(0, regularLine(character, getWidth()));
	}
	
	public void addBottom(char character) {
		lines.add(regularLine(character, getWidth()));
	}
	
	public void dump(PrintStream stream) {
		for (String line : lines) {
			stream.println(line);
		}
	}
	
	public void dump() {
		dump(System.err);
	}
}
